<template>
  <!-- 头条平台 -->
  <div class="app-container">
    <div class="all-laws">
      <el-row>
        <el-col :span="24">
          <ComSelect :options="options" @EventChange="changeFn"></ComSelect>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <ComForm
            ref="comForm"
            @EventReset="resetFn"
            @EventOnsubmit="queryFn"
            :list="comForm"
          >
          </ComForm>
        </el-col>
      </el-row>

      <ComTable
        @handleCurrentChange="currentPageChangeFn"
        @handleSizeChange="changePageFn"
        :key="romdom"
        :list="tableList"
        :modes="tableModes"
        :config="{ border: true, loading }"
        :pagination="{ total: paging.total, pageNum: paging.pageNum }"
      >
        <el-table-column label="操作" #default="{ row }" align="left">
          <template v-if="row.status == 0">
            <el-button
              @click="clickLook(row)"
              plain
              size="mini"
              type="primary"
              v-hasPermi="['base:docModule:getByType']"
              >查看</el-button
            >

            <el-button
              plain
              size="mini"
              @click="deleteFn(row)"
              v-hasPermi="['doc:top:remove']"
              >删除</el-button
            >
          </template>
          <template v-else-if="row.status === 1">
            <el-button
              plain
              size="mini"
              type="primary"
              @click="
                $router.push({
                  path: '/contentPlatform/headline/headline-details',
                  query: row,
                })
              "
              v-hasPermi="['wss:contentPlatform:headline:look1']"
              >查看</el-button
            >
          </template>
          <template v-else-if="row.status == 2">
            <el-button
              plain
              size="mini"
              type="primary"
              v-hasPermi="['work:verifyOpinion:getByVerify']"
              @click="goTODefailFn(row)"
              >失败原因</el-button
            >
          </template>
          <template v-else-if="row.status == 4">
            <el-button
              plain
              size="mini"
              type="primary"
              @click="GotoDetailFn(row)"
              >详细</el-button
            >
            <el-button plain size="mini" type="primary">数据详细</el-button>
          </template>
        </el-table-column>
      </ComTable>
    </div>
  </div>
</template>

<script>
import ComForm from "../../../components/comForm.vue";
import ComSelect from "../../../components/comSelect.vue";
import ComTable from "../../../components/comTable.vue";
// 测试
import { getdocGetTypeApi } from "@/wss/api/ContentSupervision";

// import { getTopListApi, getTopRemoveApi } from '@/wss/api/headline'
import { getTopListApi, getTopRemoveApi } from "@/wss/api/doc/top.js";
import { getListDataApi } from "@/wss/api/label";

export default {
  components: { ComSelect, ComForm, ComTable },
  dicts: ["sys_normal_disable"],

  data() {
    return {
      loading: false,
      initSelectValue: "",
      selectLabelArr: [],
      paging: {
        total: 10,
        pageNum: 1,
        pageSize: 10,
        type: "头条",
        status: "",
      },
      // 修复切换”审核不会更新bug“
      romdom: "",
      currentSelect: "",
      currentShowText: {
        first: "查看",
        second: "删除",
      },
      // 快速筛选选项
      options: {
        value: "",
        title: "快速筛选",
        list: [
          {
            value: "",
            label: "全部",
          },
          {
            value: "0",
            label: "待编辑",
          },
          {
            value: "1",
            label: "审核中",
          },
          {
            value: "2",
            label: "审核失败",
          },
          {
            //3是审核成功，但后端说上架就是成功，所以就不用3了
            value: "4",
            label: "上架",
          },
          {
            value: "5",
            label: "下架",
          },
          {
            value: "9",
            label: "编辑中",
          },
        ],
      },

      // 表格的数据
      tableList: [],
      // 表格列列的数据
      tableModes: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: "编号",
        },
        {
          label: "内容名称",
          mode: "title",
          showOverflowTooltip: true,
        },
        {
          label: "来源",
          mode: "from",
          width: 200,
        },
        {
          label: "状态",
          mode: "status",
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "待编辑",
              v: 0,
            },
            {
              l: "待审核",
              v: 1,
            },
            {
              l: "审核失败",
              v: 2,
              type: "danger",
            },
            {
              l: "上架",
              v: 4,
            },
            {
              l: "下架",
              v: 5,
            },
            {
              l: "编辑中",
              v: 9,
            },
          ],
        },
        {
          label: "创建时间",
          mode: "createTime",
        },
      ],

      comForm: {
        modes: [
          {
            placeholder: "请输入内容名称",
            formType: "input",
            mode: "title",
            label: "内容名称",
            value: "",
          },
          {
            placeholder: "请输入来源",
            formType: "input",
            mode: "from",
            label: "来源",
            value: "",
          },

          {
            placeholder: "请选择日期",
            formType: "datePicker",
            mode: "createTime",

            label: "生效日期",
            value: "",
            formType: "datePicker",
            pickerType: "datetime",
          },
        ],
      },
    };
  },
  async mounted() {
    this.getHeadlineListFn();
  },
  methods: {
    // 查看详细
    GotoDetailFn(row) {
      this.$router.push({
        path: "/contentPlatform/headline/headline-details",
        //成功接口返回的数据和其他不同，所以传递id也不同
        query: { id: this.options.value == "4" ? row.esId : row.idStr },
      });
    },
    // // 动态生成标签列表   🧨有大类太多相同的了，所以有些方法和属性移动到了 src\wss\utils\wss.js🧨

    // async getLabelFn() {
    //   let { rows } = await getListDataApi();
    //   this.comForm.modes.forEach((item) => {
    //     if (item.mode === "mark") {
    //       item.select = rows.map((item) => {
    //         return { label: item.content, value: item.idStr };
    //       });
    //     }
    //   });
    // },
    // 失败原因
    goTODefailFn(row) {
      this.$router.push({
        path: "/contentPlatform/headline/headline-failReason",
        query: { id: row.idStr },
      });
    },
    // 重设函数
    resetFn() {
      this.initSelectValue = "";
      this.romdom = Math.random();
      this.options.value = "";
      this.paging.status = "";
    },
    // 查询函数
    async queryFn(arr) {
      // 获取查询阐述
      arr.forEach((item) => {
        for (const key in item) {
          this.paging[key] = item[key];
          // 获取结束时间和开始时间
          if (item.temp && item.temp instanceof Array) {
            this.paging.beginTime = item.temp[0];
            this.paging.endTime = item.temp[1];
            this.paging.temp = null;
          }
        }
      });
      this.paging.pageNum = 1;
      // console.log(this.paging, "11111111111111");

      this.romdom = Math.random();
      if (this.options.value == 4) {
        // 成功时候需要调用另一个接口
        let { rows } = await getdocGetTypeApi(this.paging);
        this.tableList = rows;
        return;
      }
      this.getHeadlineListFn();
      // 初始筛选
      this.resetFilterFn();
    },
    // 当前页发生改变时候
    currentPageChangeFn(pageNum) {
      this.paging.pageNum = pageNum;
      this.getHeadlineListFn();
    },
    // 重新设置筛选条件

    // 页容量发生改变时候
    changePageFn(pageSize) {
      this.paging.pageSize = pageSize;
      this.getHeadlineListFn();
    },
    async getHeadlineListFn() {
      this.loading = true;

      let { data } = await getTopListApi(this.paging);
      console.log(data, "头条数据");

      this.paging.total = data.total;

      this.loading = false;
      // 拼接标签
      data.rows.forEach((item, index) => {
        if (item.mark instanceof Array) {
          item.mark = item.mark.join(" / ");
        }
      });
      // this.selectLabelArr = [];

      this.tableList = data.rows;
      // this.tableList.forEach((item) => {
      //   if (!this.selectLabelArr.includes(item.mark)) {
      //     this.selectLabelArr.push(item.mark);
      //   }
      // });

      // this.selectLabelArr = this.arrTransformObj(this.selectLabelArr);
    },
    // 删除
    async deleteFn(row) {
      this.$confirm("你确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let { msg } = await getTopRemoveApi({ id: row.id });
          this.getHeadlineListFn();
          this.$message({
            type: "success",
            message: msg,
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // GotoDetailFn() {
    //   this.$router.push({
    //     path: "/contentPlatform/headline/askAnswer-details",
    //   });
    // },
    resetFilterFn() {
      this.tableModes = [
        {
          type: "selection",
        },
        {
          type: "index",
          label: "编号",
        },
        {
          label: "内容名称",
          mode: "title",
          showOverflowTooltip: true,
        },
        {
          label: "来源",
          mode: "from",
          width: 200,
        },
        {
          label: "状态",
          mode: "status",
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "待编辑",
              v: 0,
            },
            {
              l: "待审核",
              v: 1,
            },
            {
              l: "审核失败",
              v: 2,
              type: "danger",
            },
            {
              l: "上架",
              v: 4,
            },
            {
              l: "下架",
              v: 5,
            },
          ],
        },
        {
          label: "创建时间",
          mode: "createTime",
        },
      ];
      this.comForm = {
        modes: [
          {
            placeholder: "请输入内容名称",
            formType: "input",
            mode: "title",
            label: "内容名称",
            value: "",
          },
          {
            placeholder: "请输入来源",
            formType: "input",
            mode: "from",
            label: "来源",
            value: "",
          },

          {
            placeholder: "请选择日期",
            formType: "datePicker",
            mode: "createTime",

            label: "创建时间",
            value: "",
            formType: "datePicker",
            pickerType: "datetime",
          },
        ],
      };
    },
    async currentStatusFn(value) {
      // 获取所有标签并且动态生成
      const getLabelFn = async () => {
        let { rows } = await getListDataApi();
        console.log(rows, "label");
        this.comForm.modes.forEach((item) => {
          if (item.mode === "labelId") {
            item.select = rows.map((item) => {
              return { label: item.content, value: item.idStr };
            });
          }
        });
      };
      this.resetFilterFn();
      // 审核中
      console.log(value);
      if (value == 1) {
        // 初始化筛选字段
        this.resetFilterFn();
        this.tableModes[5].label = "提交时间";
        this.tableModes[5].mode = "updateTime";
      } else if (value == 2) {
        // 初始化筛选字段
        this.resetFilterFn();
        this.tableModes[5].label = "提交时间";
        this.tableModes[5].mode = "updateTime";
        this.tableModes.push({
          label: "审核时间",
          mode: "verifyTime",
        });

        // 上架
      } else if (value == 4) {
        this.tableModes = [
          {
            type: "selection",
          },
          {
            mode: "no",
            label: "内容编号",
          },
          {
            label: "内容类型",
            mode: "type",
            showOverflowTooltip: true,
          },
          {
            label: "内容标题",
            mode: "title",
            showOverflowTooltip: true,
          },
          {
            label: "标签",
            mode: "label",
            showOverflowTooltip: true,
          },

          {
            label: "上架日期",
            mode: "updatedTime",
          },
        ];
        this.comForm = {
          modes: [
            {
              placeholder: "请输入来源",
              formType: "input",
              mode: "no",
              label: "内容编号",
              value: "",
            },
            {
              placeholder: "请输入内容名称",
              formType: "input",
              mode: "title",
              label: "内容名称",
              value: "",
            },

            {
              formType: "select",
              mode: "labelId",
              label: "内容标签",
              value: "",
              select: [],
            },
            {
              mode: "temp",
              formType: "datePicker",
              label: "提交日期",
              value: "",
              pickerType: "daterange",
              startPlaceholder: "开始日期",
              endPlaceholder: "结束日期",
            },
          ],
        };
        // 获取所有标签并且动态生成
        getLabelFn();
        // let { rows } = await getdocGetTypeApi(this.paging);
        // console.log(rows, "rowrow");
        // this.tableList = rows
      } else {
        // 初始化筛选条件
        this.resetFilterFn();
      }
    },

    // 快熟筛选发生改变时候
    async changeFn(value) {
      this.$refs.comForm.resetFn();
      this.options.value = value;
      this.currentSelect = value;
      // 更新是状态（如代编辑）
      this.paging = {
        pageNum: 1,
        pageSize: 10,
        status: "",
        type: "头条",
        total: 10,
      };
      this.paging.status = value;
      // console.log(value);
      if (value == 4) {
        // 成功时候需要调用另外一个接口
        let { rows } = await getdocGetTypeApi(this.paging);
        this.tableList = rows;
        console.log(rows, "row");
      } else {
        await this.getHeadlineListFn();
      }

      this.currentStatusFn(value, this);
      // 修复切换”审核不会更新bug“
      this.romdom = Math.random();
    },

    // 查看
    clickLook(row) {
      console.log("idStridStridStridStr", row.idStr);
      this.$router.push({
        path: "/contentPlatform/headline/headline-details",
        query: { id: row.idStr },
      });
    },
  },
};
</script>

<style lang ="scss" scoped>
.clearfix {
  padding-top: 20px;
}
</style>
